package com.ruoyi.pickup.controller;

import java.util.List;
import java.util.Date;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.pickup.domain.PickupPointApplications;
import com.ruoyi.pickup.service.IPickupPointApplicationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 团长注册申请表Controller
 * 
 * @author ruoyi
 * @date 2025-08-15
 */
@RestController
@RequestMapping("")
public class PickupPointApplicationsController extends BaseController
{
    @Autowired
    private IPickupPointApplicationsService pickupPointApplicationsService;

    /**
     * 查询团长注册申请表列表
     */
    @RequiresPermissions("pickup-applications:pickup:list")
    @GetMapping("/list")
    public TableDataInfo list(PickupPointApplications pickupPointApplications)
    {
        startPage();
        List<PickupPointApplications> list = pickupPointApplicationsService.selectPickupPointApplicationsList(pickupPointApplications);
        return getDataTable(list);
    }

    /**
     * 导出团长注册申请表列表
     */
    @RequiresPermissions("pickup-applications:pickup:export")
    @Log(title = "团长注册申请表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PickupPointApplications pickupPointApplications)
    {
        List<PickupPointApplications> list = pickupPointApplicationsService.selectPickupPointApplicationsList(pickupPointApplications);
        ExcelUtil<PickupPointApplications> util = new ExcelUtil<PickupPointApplications>(PickupPointApplications.class);
        util.exportExcel(response, list, "团长注册申请表数据");
    }

    /**
     * 获取团长注册申请表详细信息
     */
    @RequiresPermissions("pickup-applications:pickup:query")
    @GetMapping(value = "/{applicationId}")
    public AjaxResult getInfo(@PathVariable("applicationId") String applicationId)
    {
        return success(pickupPointApplicationsService.selectPickupPointApplicationsByApplicationId(applicationId));
    }

    /**
     * 新增团长注册申请表
     */
    @RequiresPermissions("pickup-applications:pickup:add")
    @Log(title = "团长注册申请表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PickupPointApplications pickupPointApplications)
    {
        return toAjax(pickupPointApplicationsService.insertPickupPointApplications(pickupPointApplications));
    }

    /**
     * 修改团长注册申请表
     */
    @RequiresPermissions("pickup-applications:pickup:edit")
    @Log(title = "团长注册申请表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PickupPointApplications pickupPointApplications)
    {
        return toAjax(pickupPointApplicationsService.updatePickupPointApplications(pickupPointApplications));
    }

    /**
     * 删除团长注册申请表
     */
    @RequiresPermissions("pickup-applications:pickup:remove")
    @Log(title = "团长注册申请表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applicationIds}")
    public AjaxResult remove(@PathVariable String[] applicationIds)
    {
        return toAjax(pickupPointApplicationsService.deletePickupPointApplicationsByApplicationIds(applicationIds));
    }

    /**
     * 审核团长注册申请
     */
    @RequiresPermissions("pickup-applications:pickup:audit")
    @Log(title = "审核团长注册申请", businessType = BusinessType.UPDATE)
    @PostMapping("/audit/{applicationId}")
    public AjaxResult audit(@PathVariable("applicationId") String applicationId, 
                           @RequestBody PickupPointApplications pickupPointApplications)
    {
        // 先查询完整的申请信息
        PickupPointApplications fullApplication = pickupPointApplicationsService.selectPickupPointApplicationsByApplicationId(applicationId);
        if (fullApplication == null) {
            return error("申请信息不存在");
        }
        
        // 更新审核相关字段
        fullApplication.setStatus(pickupPointApplications.getStatus());
        fullApplication.setReviewTime(new Date());
        fullApplication.setReviewerId(SecurityUtils.getUserId());
        fullApplication.setReviewNotes(pickupPointApplications.getReviewNotes());
        
        return toAjax(pickupPointApplicationsService.auditPickupApplication(fullApplication));
    }

    /**
     * 批量审核团长注册申请
     */
    @RequiresPermissions("pickup-applications:pickup:audit")
    @Log(title = "批量审核团长注册申请", businessType = BusinessType.UPDATE)
    @PostMapping("/batchAudit")
    public AjaxResult batchAudit(@RequestBody PickupPointApplications pickupPointApplications)
    {
        pickupPointApplications.setReviewTime(new Date());
        pickupPointApplications.setReviewerId(SecurityUtils.getUserId());
        return toAjax(pickupPointApplicationsService.batchAuditPickupApplications(pickupPointApplications));
    }
}
